package com.dq.controller.in.base;

import com.dq.exception.customException.AdminException;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/27 0027 16:16:38
 */
@Data
public class BaseQuery {
    @ApiModelProperty("页数")
    public Integer page;
    @ApiModelProperty("页长")
    public Integer size;


    public static String likeSql(String value) {
        return "%" + value + "%";
    }

    public Map<String, Object> parseMap() throws AdminException {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            for (Field param : this.getClass().getFields()) {
                String paramName = param.getName();
                param.setAccessible(true);
                Object o = param.get(this);
                if (o instanceof String) {
                    if (StringUtils.isNotBlank(o + "")) {
                        map.put(paramName, param.get(this));
                    }
                } else if (o instanceof Enum) {
                    if (!((Enum) o).name().equals("all")) {
                        map.put(paramName, param.get(this));
                    }
                } else {
                    if (o != null) {
                        map.put(paramName, param.get(this));
                    }
                }
            }
            return map;
        } catch (IllegalAccessException e) {
            throw new AdminException("查询转换失败");
        }
    }
}
